package com.da.javatest.interview.session2;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author chenlida
 * @date 2020/3/30 23:38
 * @description
 */
public class Test16SynchronousQueue {
    public static void main(String[] args) {
        BlockingQueue<String> bq = new SynchronousQueue<>();

        new Thread(() -> {
            try {
                System.out.println(Thread.currentThread().getName() + " put a");
                bq.put("a");
                System.out.println(Thread.currentThread().getName() + " put b");
                bq.put("b");
                System.out.println(Thread.currentThread().getName() + " put c");
                bq.put("c");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "A").start();

        new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getName() + " take " + bq.take());
                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getName() + " take " + bq.take());
                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getName() + " take " + bq.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "B").start();
    }
}
